<template>
	<!-- 使用单位 添加/编辑/详情 -->
	<div class="compile">
		<el-dialog :close-on-click-modal='false' :append-to-body='true' :before-close='close' :close-on-press-escape='false'
		 :title="title" :visible.sync="addAndCompileShow">
			<el-form :model="form" :rules="rules" ref="form" v-loading='isLoading'>
				<el-row>
					<div class="textLeft titleMessNow">单位信息</div>
				</el-row>
				<el-row>
					<el-col :span="10" :offset='1'>
						<el-form-item label="单位名称:" :label-width="formLabelWidth" prop="groupName">
							<el-input size="mini" v-model="form.groupName" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10" :offset='2'>
						<el-form-item label="社会信用码:" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.socialCode" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="10" :offset='1'>
						<el-form-item label="法人:" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.legalPeo" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10" :offset='2'>
						<el-form-item label="法人电话:" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.legalPeoPhone" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="10" :offset='1'>
						<el-form-item label="主要负责人" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.principalPeo" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10" :offset='2'>
						<el-form-item label="负责人电话:" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.principalPeoPhone" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row>
					<el-col :span="10" :offset='1'>
						<el-form-item label="单位电话:" :label-width="formLabelWidth">
							<el-input size="mini" v-model="form.groupPhone" autocomplete="off" clearable :disabled='detailDisable'></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>

			<div slot="footer" class="dialog-footer">
				<el-button @click="close(2)">取 消</el-button>
				<el-button type="primary" @click="submit('form')" v-loading='ensureBtn' v-if="!detailDisable">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog custom-class="imgDialogShow" :visible.sync="imgDialogShow" :append-to-body='true' :before-close='closeImg'>
			<img :src="dialogImgUrl" alt="">
		</el-dialog>
	</div>
</template>

<script>
	import {
		axiosPost,
		axiosGet,
		loadFn,
		axiosChange,
	} from "@/common.js"
	import {
		groupName,
	} from "./rule.js"
	export default {
		props: ['addAndCompileShow', 'isAddOrComp', 'rowId'],
		mounted() {
			if (this.title == '添加使用单位') {
				this.isLoading = false
			}
		},
		data() {
			return {
				formLabelWidth: '120px',
				form: {
					groupName: '',
					socialCode: '',
					legalPeo: '',
					legalPeoPhone: '',
					principalPeo: '',
					principalPeoPhone: '',
					groupPhone: '',
				},
				ensureBtn: false,
				rules: {
					groupName,
				},
				isLoading: true,
				dialogImgUrl: '',
				imgDialogShow: false,
				updataType: '',
				lastUpdataTime: '',
				recordCon: '',
				recordCon1: '',
			}
		},
		computed: {
			detailDisable() {
				if (this.title == '使用单位详情') {
					return true
				} else {
					return false
				}
			},
			title() {
				if (this.isAddOrComp == 1) {
					return '添加使用单位'
				} else if (this.isAddOrComp == 2) {
					this.getCompileList()
					return '编辑使用单位'
				} else {
					this.getCompileList()
					return '使用单位详情'
				}
			},
			roleId() {
				if (localStorage.role) {
					return JSON.parse(localStorage.role).id
				} else {
					return ''
				}
			},
		},
		methods: {
			copy() {
				/* 创建range对象   */
				const range = document.createRange();
				range.selectNode(document.getElementById('content'));
				/* 窗口的selection对象，表示用户选择的文本 */
				const selection = window.getSelection();
				if (selection.rangeCount > 0) selection.removeAllRanges(); // 将已经包含的已选择的对象清除掉
				selection.addRange(range); // 将要复制的区域的range对象添加到selection对象中
				document.execCommand('copy'); // 执行copy命令，copy用户选择的文本
			},
			copy1() {
				/* 创建range对象   */
				const range = document.createRange();
				range.selectNode(document.getElementById('content1'));
				/* 窗口的selection对象，表示用户选择的文本 */
				const selection = window.getSelection();
				if (selection.rangeCount > 0) selection.removeAllRanges(); // 将已经包含的已选择的对象清除掉
				selection.addRange(range); // 将要复制的区域的range对象添加到selection对象中
				document.execCommand('copy'); // 执行copy命令，copy用户选择的文本
			},
			async oprationUintPub(value) {
				let url = ''
				let method = ''
				let rowId = this.rowId ? this.rowId : ''
				let data = {
					name: this.form.groupName, //单位名称
					unified_code: this.form.socialCode, //社会统一信用码
					legal_person: this.form.legalPeo, //法人
					legal_phone: this.form.legalPeoPhone, //法人电话
					person_in_charge: this.form.principalPeo, //主要负责人
					person_in_charge_phone: this.form.principalPeoPhone, //负责人电话
					company_telephone: this.form.groupPhone, //单位电话
					company_type: 300,
				}
				if (value == 1) {
					url = '/user/company/'
					method = 'post'
				} else {
					url = '/user/company/' + rowId + '/'
					method = 'put'
				}
				setTimeout(() => {
					axiosChange(url, method, data, (res) => {
						this.ensureBtn = false
						if (res.code == 0) {
							this.close(1)
							this.$message({
								type: 'success',
								message: '操作成功'
							})
						} else {
							this.$message.error(res.detail.result)
						}
					})
				}, 10)
			},
			//获取编辑 详情数据
			getCompileList() {
				let rowId = this.rowId ? this.rowId : ''
				let url = '/user/company/' + rowId + '/'
				axiosGet(url, {}, (res) => {
					this.isLoading = false
					if (res.code == 0) {
						this.form.groupName = res.detail.name //单位名称
						this.form.socialCode = res.detail.unified_code //社会统一信用码
						this.form.legalPeo = res.detail.legal_person //法人
						this.form.legalPeoPhone = res.detail.legal_phone //法人电话
						this.form.principalPeo = res.detail.person_in_charge //主要负责人
						this.form.principalPeoPhone = res.detail.person_in_charge_phone //负责人电话
						this.form.groupPhone = res.detail.company_telephone //单位电话
					} else {
						this.$message.error(res.detail.result)
					}
				})
			},
			//提交
			submit(ruleForm) {
				this.$refs[ruleForm].validate((valid) => {
					if (valid) {
						this.ensureBtn = true
						if (this.isAddOrComp == 1) {
							this.oprationUintPub(1)
						} else {
							this.oprationUintPub(2)
						}
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			},
			//关闭
			close(value) {
				let newValue = 2
				if (value) {
					newValue = value
				}
				this.$emit('detailMaskShow', newValue)
			},
			closeImg() {
				this.imgDialogShow = false
			},
		}
	}
</script>
<style scoped lang="less">
</style>
